package com.goldplusgold.tradedelay.app.home.live.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.goldplusgold.live.LVDataManager;
import com.goldplusgold.live.bean.AnswerBean;
import com.goldplusgold.live.result.ResultAnswers;
import com.goldplusgold.live.result.ResultLiveQuestions;
import com.goldplusgold.live.result.ResultVideoAnswers;
import com.goldplusgold.live.result.ResultVideoQuestions;
import com.example.account.manager.AppCacheSharedPreferences;
import com.goldplusgold.support.lib.utils.Logger;
import com.goldplusgold.support.lib.widget.DividerItemDecoration;
import com.goldplusgold.support.lib.widget.recyclerview.RecyclerViewExtended;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.base.widget.QuitDialog;
import com.goldplusgold.tradedelay.app.home.live.LiveDetailActivity;
import com.goldplusgold.tradedelay.app.home.live.adapter.AnswerAdapter;
import com.example.account.manager.ParmasKey;
import com.goldplusgold.tradedelay.app.home.live.base.LiveBaseCallBack;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/7/17.
 */

public class AnswerFragment extends Fragment {
    private static boolean mloading = false ; //正在加载中...
    //加载的页数
    private int mPageNum = 0;
    private int mPageSize = 10;
    //选择的Tab默认为 0 ；
    private int mSelectTab = 0;

    private int mLastNum = 0;
    private RelativeLayout view;
    private RecyclerViewExtended mRecyclerView;
    TextView all_ask_answers_btn;
    TextView my_questions_btn;
    LinearLayout my_questions_bottom_layout;
    EditText editText;
    TextView send;
    TextView switchBtn;
    String liveID;
    Context context;
    AnswerAdapter answerViewAdapter;
    AnswerAdapter questionViewAdapter;
    ChangeTabInterface changeTabInterface;
    TextView hint_text;
    int type;

    public AnswerFragment(Context context, String liveID, ChangeTabInterface changeTabInterface, int type) {
        this.liveID = liveID;
        this.context = context;
        this.type = type;
        this.changeTabInterface = changeTabInterface;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view =
                (RelativeLayout) inflater.inflate(R.layout.fragment_live_tab, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mRecyclerView = (RecyclerViewExtended) view.findViewById(R.id.answers_recycler_view);
        hint_text = (TextView) view.findViewById(R.id.hint_text);
        all_ask_answers_btn = (TextView) view.findViewById(R.id.all_ask_answers_btn);
        all_ask_answers_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mSelectTab != 0) {
                    swithAdapter(0);
                }
            }
        });
        my_questions_btn = (TextView) view.findViewById(R.id.my_questions_btn);
        my_questions_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mSelectTab != 1) {
                    swithAdapter(1);
                }
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mRecyclerView.getContext()));

        //初始化两个adapter;
        answerViewAdapter = new AnswerAdapter(context);
        questionViewAdapter = new AnswerAdapter(context);

        //默认设置为answerViewAdapter
        mRecyclerView.setAdapter(answerViewAdapter);

        mRecyclerView.addItemDecoration(new DividerItemDecoration(this.getContext(),
                DividerItemDecoration.VERTICAL_LIST));

        requestAnswers();

        mRecyclerView.setOnLoadMoreListener(new RecyclerViewExtended.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                switch (mSelectTab) {
                    case 0:
                        requestAnswers();
                        break;
                    case 1:
                        requestQuestions();
                        break;
                    default:
                        break;
                }
            }
        });
    }

    private void swithAdapter(int id) {
        changeTab(id);
        mPageNum = 0; //每次切换时 mPageNum 为0.
        mLastNum = 0;
        if (id == 0) {
            mSelectTab = 0;
            answerViewAdapter.setDatas(new ArrayList<AnswerBean>());
            all_ask_answers_btn.setTextColor(this.getResources().getColor(R.color.white));
            all_ask_answers_btn.setBackgroundResource(R.drawable.shape_left_select_answer_tab);
            my_questions_btn.setTextColor(this.getResources().getColor(R.color.color_gold));
            my_questions_btn.setBackgroundResource(R.drawable.shape_right_normal_answer_tab);
            hint_text.setVisibility(View.GONE);
            mRecyclerView.setAdapter(answerViewAdapter);
            requestAnswers();

        }
        if (id == 1) {
            mSelectTab = 1;
            if (AppCacheSharedPreferences.getCacheBoolean(ParmasKey.login_state , false)) {
                answerViewAdapter.setDatas(new ArrayList<AnswerBean>());
                hint_text.setVisibility(View.GONE);
                mRecyclerView.setAdapter(questionViewAdapter);
                requestQuestions();
                all_ask_answers_btn.setTextColor(this.getResources().getColor(R.color.color_gold));
                all_ask_answers_btn.setBackgroundResource(R.drawable.shape_left_normal_answer_tab);
                my_questions_btn.setTextColor(this.getResources().getColor(R.color.white));
                my_questions_btn.setBackgroundResource(R.drawable.shape_right_select_answer_tab);
            } else {
                mSelectTab = 0 ;

                QuitDialog dialog = new QuitDialog(context);
                dialog.show();
            }
        }
    }

    private void changeTab(int id) {
        if (changeTabInterface != null) {
            changeTabInterface.onTab(id);
        }
    }

    private void refreshQuestionsView(List<AnswerBean> datas) {
        if (datas == null || datas.size() == 0) {
            if (mPageNum == 0) {
                questionViewAdapter.setDatas(new ArrayList<AnswerBean>(datas));
                hint_text.setVisibility(View.VISIBLE);
            } else {
                questionViewAdapter.addDatas(new ArrayList<AnswerBean>(datas));
                hint_text.setVisibility(View.GONE);
            }
            mRecyclerView.onLoadCompletedAction(false);
        } else {
            hint_text.setVisibility(View.GONE);
            if (mPageNum == 0) {
                questionViewAdapter.setDatas(new ArrayList<AnswerBean>(datas));
            } else {
                questionViewAdapter.addDatas(new ArrayList<AnswerBean>(datas));
            }
            mRecyclerView.onLoadCompletedAction(mPageNum, mPageSize, datas.size());
            mPageNum = mPageNum + datas.size();
        }

        questionViewAdapter.notifyChange();
    }

    private void refreshAnswersView(List<AnswerBean> answers) {
        if (answers == null || answers.size() == 0) {

            if (mPageNum == 0) {
                answerViewAdapter.setDatas(new ArrayList<AnswerBean>());
                hint_text.setVisibility(View.VISIBLE);
            } else {
                answerViewAdapter.addDatas(new ArrayList<AnswerBean>());
                hint_text.setVisibility(View.GONE);
            }
            mRecyclerView.onLoadCompletedAction(false);
        } else {
            hint_text.setVisibility(View.GONE);

            if (mPageNum == 0) {
                answerViewAdapter.setDatas(new ArrayList<AnswerBean>(answers));
            } else {
                answerViewAdapter.addDatas(new ArrayList<AnswerBean>(answers));
            }
            mRecyclerView.onLoadCompletedAction(mPageNum, mPageSize, answers.size());
            mPageNum = mPageNum + answers.size();
        }

        answerViewAdapter.notifyChange();
    }

    public void requestQuestions() {
        if(mloading == false) {
            mloading = true ;
            if (type == LiveDetailActivity.LiveDetailType.VIDEO) {
                LVDataManager.getInstance().getVideoQuestions(liveID, mPageNum, mPageSize, new LiveBaseCallBack<ResultVideoQuestions>(context) {
                    @Override
                    public void onResponse(Call<ResultVideoQuestions> call, final Response<ResultVideoQuestions> response) {
                        mloading = false ;
                        super.onResponse(call , response);
                        if (answerViewAdapter != null && response.body().getDatas() != null) {
                            refreshQuestionsView(response.body().getDatas());
                        }else{

                        }

                    }

                    @Override
                    public void onFailure(Call<ResultVideoQuestions> call, Throwable t) {
                        Logger.e("getVideoQuestions", t.toString());
                        mloading = false ;
                    }
                });
            } else {
                LVDataManager.getInstance().getLiveQuestions(liveID, mPageNum, mPageSize, new LiveBaseCallBack<ResultLiveQuestions>(getActivity()) {
                    @Override
                    public void onResponse(Call<ResultLiveQuestions> call, final Response<ResultLiveQuestions> response) {
                        mloading = false ;
                        super.onResponse(call , response);
                        if (answerViewAdapter != null && response.body().getDatas() != null) {
                            refreshQuestionsView(response.body().getDatas());
                        }else{

                        }

                    }

                    @Override
                    public void onFailure(Call<ResultLiveQuestions> call, Throwable t) {
                        mloading = false ;
                        super.onFailure(call , t);
                        Logger.e("getLiveQuestions", t.toString());
                    }
                });
            }
        }

    }

    private void requestAnswers() {
        if(mloading == false) {
            mloading = true ;
            if (type == LiveDetailActivity.LiveDetailType.VIDEO) {
                LVDataManager.getInstance().getVideoAnswers(liveID, mPageNum, mPageSize, new Callback<ResultVideoAnswers>() {
                    @Override
                    public void onResponse(Call<ResultVideoAnswers> call, final Response<ResultVideoAnswers> response) {
                        mloading = false ;
                        if (answerViewAdapter != null) {
                            refreshAnswersView(response.body().getAnswers());
                        }

                    }

                    @Override
                    public void onFailure(Call<ResultVideoAnswers> call, Throwable t) {
                        Logger.e("getVideoAnswers", t.toString());
                        mloading = false ;
                    }
                });
            } else {
                LVDataManager.getInstance().getLiveAnswers(liveID, mPageNum, mPageSize, new Callback<ResultAnswers>() {
                    @Override
                    public void onResponse(Call<ResultAnswers> call, final Response<ResultAnswers> response) {
                        mloading = false ;
                        if (answerViewAdapter != null) {
                            if(response.body() == null){
                                return;
                            }
                            if(response.body().getAnswers() == null){
                                return;
                            }
                            refreshAnswersView(response.body().getAnswers());
                        }
                    }

                    @Override
                    public void onFailure(Call<ResultAnswers> call, Throwable t) {
                        mloading = false ;
                        Logger.e("getLiveAnswers", t.toString());
                    }
                });
            }
        }

    }

    public void requestQuestions(int fromUpData){
        if(fromUpData == 1){
            mPageNum = 0 ;
            requestQuestions();
        }
    }

    public interface ChangeTabInterface {
        void onTab(int id);
    }
}
